package com.itheima.NumberBomb;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GameLogic {
    // 游戏范围和目标数字
    private int minRange;
    private int maxRange;
    private int targetNumber;
    private boolean isGameActive;
    private List<String> guessHistory;
    
    public GameLogic() {
        initializeGame();
    }
    
    // 初始化游戏参数
    public void initializeGame() {
        minRange = 1;
        maxRange = 1000;
//        targetNumber = (int) (Math.random() * 1000) + 1;
        Random r = new Random();
        int targetNumber = r.nextInt(1000)+1;
        isGameActive = true;
        guessHistory = new ArrayList<>();
    }
    
    // 处理用户猜测
    public GuessResult processGuess(int guess) {
        if (!isGameActive) {
            return new GuessResult(ResultType.GAME_OVER, "游戏已结束，请点击重新开始", minRange, maxRange);
        }
        
        // 验证输入范围
        if (guess < minRange || guess > maxRange) {
            return new GuessResult(ResultType.INVALID_RANGE, 
                    "请输入" + minRange + "-" + maxRange + "之间的数字", 
                    minRange, maxRange);
        }
        
        // 检查猜测结果
        if (guess == targetNumber) {
            isGameActive = false;
            String historyEntry = "猜对了! 答案是: " + targetNumber;
            guessHistory.add(historyEntry);
            return new GuessResult(ResultType.CORRECT, 
                    "恭喜你，中奖了！数字炸弹是: " + targetNumber, 
                    minRange, maxRange);
        } else if (guess < targetNumber) {
            minRange = guess + 1;
            String historyEntry = "猜测: " + guess + " - 太小了，新范围: " + minRange + "-" + maxRange;
            guessHistory.add(historyEntry);
            return new GuessResult(ResultType.TOO_LOW, 
                    "太小了！再试一次", 
                    minRange, maxRange);
        } else {
            maxRange = guess - 1;
            String historyEntry = "猜测: " + guess + " - 太大了，新范围: " + minRange + "-" + maxRange;
            guessHistory.add(historyEntry);
            return new GuessResult(ResultType.TOO_HIGH, 
                    "太大了！再试一次", 
                    minRange, maxRange);
        }
    }
    
    // 获取历史记录文本
    public String getHistoryText() {
        StringBuilder historyText = new StringBuilder();
        for (String entry : guessHistory) {
            historyText.append(entry).append("\n");
        }
        return historyText.toString();
    }
    
    // getter方法
    public int getMinRange() {
        return minRange;
    }
    
    public int getMaxRange() {
        return maxRange;
    }
    
    public boolean isGameActive() {
        return isGameActive;
    }
    
    // 猜测结果类型枚举
    public enum ResultType {
        CORRECT, TOO_LOW, TOO_HIGH, INVALID_RANGE, GAME_OVER
    }
    
    // 猜测结果封装类
    public static class GuessResult {
        private ResultType resultType;
        private String message;
        private int newMin;
        private int newMax;
        
        public GuessResult(ResultType resultType, String message, int newMin, int newMax) {
            this.resultType = resultType;
            this.message = message;
            this.newMin = newMin;
            this.newMax = newMax;
        }
        
        // getter方法
        public ResultType getResultType() {
            return resultType;
        }
        
        public String getMessage() {
            return message;
        }
        
        public int getNewMin() {
            return newMin;
        }
        
        public int getNewMax() {
            return newMax;
        }
    }
}
    